﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wpf.CommonTools.BooleanExtension
{
    public static class BooleanExtension
    {
        /// <summary>
        /// 不会抛出异常
        /// 使用TryParse转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ToBoolean(this string str, bool defaultValue = false)
        {
            bool a;
            bool b = bool.TryParse(str, out a);
            return b ? a : defaultValue;
        }
        /// <summary>
        /// 不会抛出异常
        /// 使用TryParse转化,如果空字符串返回null值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool? ToBooleanNullable(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return null;
            }
            else
            {
                bool a;
                bool b = bool.TryParse(str, out a);
                return a;
            }
        }

    }
}
